package en.ameen.preciousengine;


/*!
A special type of colliders, that has been implemented from the interface 
Collider, it is meant to work rectangles.. obviously!
*/
public class RectangleCollider implements Collider {

	//! Position of the collider on the x-axis
	public int x;
	
	//! Position of the collider on the y-axis
	public int y;
	
	//! Width of the collider
	public int w;
	
	//! Height of the collider
	public int h;
	
	
    //! RectangleCollider Constructor
    /*!
      The constructor of the timer would set everything according to the values
      you supply
      
      \param x position of the collider on the x-axis
      \param y position of the collider on the y-axis
      \param w width of the collider
      \param h height of the collider
    */
	public RectangleCollider(int x, int y, int w, int h) {

		this.x = x;
		this.y = y;
		this.w = w;
		this.h = h;
		
	}

    //! Function to check if a collision happened between the local rectangle collider and another collider
    /*!
      
      \param other the other collider to check the collision with, for the time
      being I just wrote the collision with another rectangle collider but I'm
      sure I'm supporting sphere and other shapes collision.
      \return Flag that will indicate if there is a collision or not
    */
	public boolean isCollision(Collider other) {


		RectangleCollider otherRecCol = (RectangleCollider)other;

		//all cases where there is no collision
		
		int left1, left2;
	    int right1, right2;
	    int top1, top2;
	    int bottom1, bottom2;

	    left1 = x;
	    left2 = otherRecCol.x;
	    right1 = x + w;
	    right2 = otherRecCol.x + otherRecCol.w;
	    top1 = y;
	    top2 = otherRecCol.y;
	    bottom1 = y + h;
	    bottom2 = otherRecCol.y + otherRecCol.h;

	    if (bottom1 < top2)
	    	return false;
	    
	    if (top1 > bottom2)
	    	return false;

	    if (right1 < left2)
	    	return false;
	    
	    if (left1 > right2)
	    	return false;

	  
	    return true;
	    

	
	}

    //! Function to check if the point is inside the rectangle collider
    /*!
      
      \param x position of the point on the x-axis
      \param y position of the point on the y-axis
      \return Flag if the point is inside the rectangle collider

    */
	public boolean isPointInside(int x, int y) {

		if (x > this.x && x < this.x + w)
			if (y > this.y && y < this.y + h)
				return true;
		
		return false;
	}

	
    //! Function to update the rectangle collider position and dimensions from an entity
    /*!

      \param entityToUpdateFrom entity to copy position and dimensions from

    */
	public void updateCollider(Entity entityToUpdateFrom) {

		x = (int) entityToUpdateFrom.x;
		y = (int) entityToUpdateFrom.y;
		w = (int) entityToUpdateFrom.width;
		h = (int) entityToUpdateFrom.height;
		
		
	}


}
